const noteService = require("../services/note");

const noteController = {
  // 新增留言/照片
  addNote: async (req, res) => {
    const { type, content, user_name, user_id, label, color, image_url } =
      req.body;
    const create_time = new Date();
    const like_count = 0;
    const comment_count = 0;

    try {
      await noteService.addNote(
        type,
        content,
        user_name,
        user_id,
        label,
        color,
        create_time,
        image_url,
        like_count,
        comment_count
      );

      res.send({
        code: 200,
        message: "成功",
      });
    } catch (err) {
      res.send({
        code: 201,
        error: err.message,
        message: "添加失败",
      });
    }
  },
  // 删除留言/照片
  deleteNote: async (req, res) => {
    const { id } = req.query;
    try {
      await noteService.deleteNote(id);
      res.send({
        code: 200,
        message: "成功",
      });
    } catch (err) {
      res.send({
        code: 201,
        message: err.message,
      });
    }
  },
  // 获取留言/照片列表
  getNoteList: async (req, res) => {
    const { type, sort, label, limit, page } = req.query;

    try {
      const [data] = await noteService.getNoteList(
        type,
        sort,
        label,
        limit,
        page
      );
      res.send({
        code: 200,
        message: "成功",
        data: data,
      });
    } catch (err) {
      res.send({
        code: 201,
        message: err.message,
      });
    }
  },
  // 获取留言/照片详情
  getNote: async (req, res) => {
    const { id } = req.query;

    try {
      const [data] = await noteService.getNote(id);
      res.send({
        code: 200,
        message: "成功",
        data: data[0],
      });
    } catch (err) {
      res.send({
        code: 201,
        message: err.message,
      });
    }
  },
  // 获取反馈列表
  getFeedbackList: async (req, res) => {
    const { type } = req.query;
    try {
      const [data] = await noteService.getFeedbackList(type);
      res.send({
        code: 200,
        message: "成功",
        data: data,
      });
    } catch (err) {
      res.send({
        code: 201,
        error: err.message,
        message: "添加失败",
      });
    }
  },
  // 新增反馈
  addFeedback: async (req, res) => {
    const { wall_id, user_id, type, like_count } = req.body;
    const create_time = new Date();
    const like = type == 0 ? like_count * 1 + 1 * 1 : null;
    try {
      await noteService.addFeedback(wall_id, user_id, type, create_time, like);
      res.send({
        code: 200,
        message: "成功",
      });
    } catch (err) {
      res.send({
        code: 201,
        error: err.message,
        message: "添加失败",
      });
    }
  },
  // 删除反馈
  deleteFeedback: async (req, res) => {
    const { user_id, like_count, wall_id } = req.query;

    let count = like_count * 1 - 1 * 1;
    try {
      await noteService.deleteFeedback(user_id, count, wall_id);
      res.send({
        code: 200,
        message: "成功",
      });
    } catch (err) {
      res.send({
        code: 201,
        error: err.message,
        message: "删除失败",
      });
    }
  },
  // 新增评论
  addComment: async (req, res) => {
    const { wall_id, user_id, user_name, avatar_url, comment, comment_count } =
      req.body;
    const create_time = new Date();
    let count = comment_count * 1 + 1 * 1;
    try {
      await noteService.addComment(
        wall_id,
        user_id,
        user_name,
        avatar_url,
        comment,
        create_time,
        count
      );

      res.send({
        code: 200,
        message: "成功",
      });
    } catch (err) {
      res.send({
        code: 201,
        error: err.message,
        message: "添加失败",
      });
    }
  },
  // 获取评论列表
  getCommentList: async (req, res) => {
    const { wall_id, limit, page } = req.query;

    try {
      const [data] = await noteService.getCommentList(wall_id, limit, page);
      res.send({
        code: 200,
        message: "成功",
        data: data,
      });
    } catch (err) {
      res.send({
        code: 201,
        message: err.message,
      });
    }
  },
  // 删除评论
  deleteComment: async (req, res) => {
    const { id, comment_count, wall_id } = req.query;
    let count = comment_count * 1 - 1 * 1;
    try {
      await noteService.deleteComment(id, wall_id, count);
      res.send({
        code: 200,
        message: "成功",
      });
    } catch (err) {
      res.send({
        code: 201,
        error: err.message,
        message: "删除失败",
      });
    }
  },
  // 是否反馈
  isFeedback: async (req, res) => {
    const { user_id, wall_id, type } = req.query;
    try {
      const [data] = await noteService.isFeedback(user_id, wall_id, type);
      res.send({
        code: 200,
        message: "成功",
        data: data[0].count,
      });
    } catch (err) {
      res.send({
        code: 201,
        error: err.message,
        message: "删除失败",
      });
    }
  },
  // 上传图片
  upload: async (req, res) => {
    if (req.file) {
      res.send({
        code: 200,
        message: "成功",
        data: {
          file_url: `http://127.0.0.1:3000/picture/${req.file.filename}`,
        },
      });
    } else {
      res.send({
        code: 201,
        message: "上传失败",
      });
    }
  },
};

module.exports = noteController;
